<div class="layui-fluid" id="VIEW-group" lay-title="组织机构管理">

    <div class="layui-card" id="group_add" style="display: none;">
            <div class="layui-card-body" style="padding: 15px;">
                <form class="layui-form" action="" lay-filter="form-group">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="xing">*</span>上级机构:</label>
                            <div class="layui-input-block">
                                <ul id="OrganizationTree" name="OrganizationTree" class="dtree" data-id="0"></ul>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="xing">*</span>机构名称:</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" placeholder="机构名称" lay-verify="required" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit="" lay-filter="form-group-submit">保存</button>
                            <button type="reset" class="layui-btn layui-btn-primary">清除</button>
                        </div>
                    </div>
                </form>
            </div>
    </div>

    <div class="layui-card" id="group_edit" style="display: none;">
            <div class="layui-card-body" style="padding: 15px;">
                <form class="layui-form" action="" lay-filter="form-group">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="xing">*</span>机构名称:</label>
                            <div class="layui-input-inline">
                                <input type="text" id="name_edit" name="name_edit" placeholder="机构名称" lay-verify="required" autocomplete="off" class="layui-input">
                                <input type="hidden" id="id_edit" name="id_edit"/>
                                <input type="hidden" id="enAbbr_edit" name="enAbbr_edit"/>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit="" lay-filter="form-group-edit">确认修改</button>
                            <button type="reset" class="layui-btn layui-btn-primary">清除</button>
                        </div>
                    </div>
                </form>
            </div>
    </div>

    <div class="layui-card">
        <div class="layui-card-header">
            <i class="layui-icon layui-icon-earth"></i>&nbsp;组织机构
        </div>
        <div class="layui-card-body">
            <div class="layui-form-item">
                <table class="layui-hide" id = "group-table" lay-filter="group-table"></table>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['admin', 'table', 'form', 'dropdown', 'jquery','laydate','dtree','treetable'], function (admin, table, form, dropdown, $) {
        var view = $('#VIEW-list-table');
        var $ = layui.$
            , admin = layui.admin
            , element = layui.element
            , layer = layui.layer
            , dtree = layui.dtree
            , treetable = layui.treetable
            , laydate = layui.laydate
            , form = layui.form
            ,addTree
            ,index
            ,editTree;;
        form.render();

        /* 监听提交 */
        form.on('submit(form-group-submit)', function (data) {
            admin.post({
                api: "addOrganization",
                data:{
                    "parentId":data.field.OrganizationTree_select_nodeId,
                    "name":data.field.name,
                },
                //请求成功
                success:function(res){
                    layer.close(index);
                    layer.msg(res.data);
                    renderTable();
                }
            });
            return false;
        });
        /* 监听修改提交 */
        form.on('submit(form-group-edit)', function (data) {
            admin.put({
                api: "changeOrganization",
                data:{
                    "id": data.field.id_edit,
                    "name":data.field.name_edit,
                    "enAbbr":data.field.enAbbr_edit
                },
                //请求成功
                success:function(res){
                    layer.close(layer.index);
                    layer.msg(res.data);
                    renderTable();
                }
            });
            return false;
        });

        //渲染表格
        var renderTable = function(){
            layer.load(2);  //加载层
            treetable.render({
                height: 'full-160',
                id:'Organization_Tree',
                treeColIndex: 1,	//树形图标显示在第几列
                treeSpid: '0',		//最上级的父级id
                treeIdName: 'id',	//id字段的名称
                treePidName: 'parentId',	//父级节点字段
                treeDefaultClose: false,	//是否默认折叠
                treeLinkage: true,		//父级展开时是否自动展开所有子级
                elem: '#group-table',	//表格id
                url: 'api/v1//ucenter/sysOrganization/queryAllOrganization',
                page: true,
                cols: [[
                    {type: 'numbers', title: '编号'},
                    {field: 'id', title: '组织机构'},
                    {field: 'parentId' , title: '上级id'},
                    {field: 'name' , title: '机构名称'},
                    {field: 'enAbbr' , title: '机构备注'},
                    {templet: complain, title: '操作'}
                ]],
                //数据渲染完的回调
                done: function () {
                    //关闭加载
                    layer.closeAll('loading');
                }
            })
        };
        renderTable();
        function complain(d){//操作中显示的内容
            if(d.parentId!=0) {
                return [
                    '<a class="layui-btn layui-btn-xs" style="background-color: #00d57b;" lay-event="add"><i class="layui-icon">&#xe654;</i>新增</a>',
                    '<a class="layui-btn layui-btn-xs layui-btn-xs" lay-event="edit"><i class="layui-icon">&#xe83f;</i>编辑</a>',
                    '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</a>',
                ].join('');
            }else{
                return [
                    '<a class="layui-btn layui-btn-xs" style="background-color: #00d57b;" lay-event="add"><i class="layui-icon">&#xe654;</i>新增</a>',
                    '<a class="layui-btn layui-btn-xs layui-btn-xs" lay-event="edit"><i class="layui-icon">&#xe83f;</i>编辑</a>',
                ].join('');
            }
        }


        table.on('tool(group-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'add') {
                if(addTree == undefined){
                    addTree = dtree.renderSelect({
                        elem: "#OrganizationTree",
                        url: "api/v1//ucenter/sysOrganization/getOrganizationlist",
                        selectValue: "", // 你可以在这里指定默认值
                        skin: "layui",
                        initLevel: "20", //默认为2
                        line: true,
                        dataFormat: "list",  //配置data的风格为list
                        done: function(){
                            dtree.click(addTree, data.id); // 会自动帮你触发一次对应Id的节点的点击事件
                            $("div[dtree-click='itemNodeClick'][dtree-id='OrganizationTree'][data-id='"+data.id+"']").click();
                        }
                    });
                }else{
                    dtree.click(addTree, data.id); // 会自动帮你触发一次对应Id的节点的点击事件
                    $("div[dtree-click='itemNodeClick'][dtree-id='OrganizationTree'][data-id='"+data.id+"']").click();
                }
                index = layer.open({
                    type: 1
                    ,anim: 5
                    ,shade: 0
                    ,scrollbar: false
                    ,area:['400px','250px']
                    ,title:'新增数据'
                    ,content: $('#group_add')
                });
            } else if (layEvent === 'del') {
                layer.confirm('是否进行级联删除'+data.name+'？', {
                    btn: ['确认','取消'] //按钮
                }, function(){
                    admin.delete({
                        api: "deleteOrganization",
                        data: {
                            oid:data.id
                        },
                        success: function(res) {
                            layer.msg(res.data);
                            renderTable();
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                index = layer.open({
                    type: 1
                    ,anim: 5
                    ,shade: 0
                    ,maxmin: false
                    ,scrollbar: false
                    ,area:['400px','200px']
                    ,title:'修改数据'
                    ,content: $('#group_edit')
                });
                $('#id_edit').val(data.id);
                $('#enAbbr_edit').val(data.enAbbr);
                $('#name_edit').val(data.name);
                $('#memo_edit').val(data.memo);
            }
        });

    })
</script>